package com.itheima.es;
import com.alibaba.fastjson.JSON;
import com.itheima.EsApplication;
import com.itheima.esdemo.mapper.SkuMapper;
import com.itheima.esdemo.pojo.Sku;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.List;

@SpringBootTest(classes = EsApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ESText {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private RestHighLevelClient client;
    @Before
    public void ClientInit(){
        client=new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(
                                "192.168.200.129",9200,"http"
                        )
                )
        );
    }
    @Test
    public void ESaddSku() throws IOException {
//        创建CreateIndexRequest,指定索引库名
        CreateIndexRequest request = new CreateIndexRequest("sku");
//        指定mapping配置
        request.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"id\":{\n" +
                "        \"type\": \"text\"\n" +
                "      },\n" +
                "      \"name\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"index\": true,\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      },\n" +
                "      \"price\":{\n" +
                "        \"type\": \"integer\",\n" +
                "        \"index\": true\n" +
                "      },\n" +
                "      \"num\":{\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"image\":{\n" +
                "        \"type\": \"text\"\n" +
                "      },\n" +
                "      \"createTime\":{\n" +
                "        \"type\": \"date\"\n" +
                "      },\n" +
                "      \"categoryId\":{\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"categoryName\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"index\": true,\n" +
                "        \"analyzer\": \"ik_smart\"\n" +
                "      },\n" +
                "      \"brandName\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"index\": true,\n" +
                "        \"analyzer\": \"ik_smart\"\n" +
                "      }\n" +
                "    }\n" +
                "  }",XContentType.JSON);
//        发起请求，得到响应
            CreateIndexResponse  response=client.indices().create(request, RequestOptions.DEFAULT);

//        创建BulkRequest对象
        BulkRequest bulkRequest = new BulkRequest();
        List<Sku> list = skuMapper.findAll();
        for (Sku sku : list) {
            bulkRequest.add(new IndexRequest("sku")
            .id(sku.getId())
            .source(JSON.toJSONString(sku),XContentType.JSON));
        }
//        发起请求
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse);
    }
    @After
    public void close() throws IOException {
        client.close();
    }
}
